// Interceptor.attach(
//     ObjC.classes['NSConcreteMutableData']['- appendBytes:length:'].implementation,
//     {
//         onEnter(args) {
//             console.log(`-[NSConcreteMutableData appendBytes:${args[2]} length:${args[3].toInt32()}]`);
//             console.log(hexdump(args[2], {length: args[3].toInt32()}))


//         }

//     }
// )

Interceptor.attach(
    ObjC.classes['MPCryptoService']['- generateRandom:'].implementation,
    {
        onEnter(args) {
            // console.log(`-[NSConcreteMutableData appendBytes:${args[2]} length:${args[3].toInt32()}]`);
            // console.log(hexdump(args[2], {length: args[3].toInt32()}))


        },
        onLeave(ret){
            let a = new ObjC.Object(ret);
            console.log('generateRandom', a)
        }

    }
)


// Interceptor.attach(
//     ObjC.classes['DTURLRequestOperation']['- rpcEncrypt:'].implementation,
//     {
//         onEnter(args){
//             let a = args[2];
//             console.log('rpcEncrypt',new ObjC.Object(a))
//         },
//         onLeave(ret){
//             console.log('rpcEncrypt leave', new ObjC.Object(ret))
//         }
//     }
// )
// //
Interceptor.attach(
    ObjC.classes['DTURLRequestOperation']['- setDeKey:'].implementation,
    {
        onEnter(args){
            let a = new ObjC.Object(args[2]);
            console.log('setDeKey',a, 'typeof ',a.$className )
        }
    }
)
// Interceptor.attach(
//     ObjC.classes['MPCryptoService']['- enCryptRPC:type:secKey:secData:deKey:'].implementation,
//     {
//         onEnter(args){
//             let a1 = new ObjC.Object(args[2]);
//             console.log('encryptrpc',  hexdump(a1), 'typeof ',a1.$className )
//             console.log(args[3])
//             this.a5 = args[4];
//             // console.log('res',ObjC.Object(this.a5))
//
//         },
//
//
//         onLeave(ret){
//             console.log('leave')
//             console.log(hexdump(this.a5))
//         }
//     }
// )


var base = Module.getBaseAddress('MTPotal')

let sub_1024A59A4 = base.add(0x024A59A4);

Interceptor.attach(sub_1024A59A4, {
    onEnter(args){
        console.log('sub_1024A59A4', args[0], args[1], args[2])
        let enKey = args[1];
        let enLen = args[2];
        let secData = args[3];
        let secLen = args[4];

        let type = args[5]
        console.log('type', type)

        this.a6 = args[6]
        this.a7 = args[7]
        console.log(enKey, enLen)
        console.log('de', hexdump(enKey))
        console.log('enKey', hexdump(args[1], {
            length:16
        }))
    
        console.log('secLen', secLen)
        console.log('secdata', hexdump(secData))
        console.log('secData', hexdump(secData, {
            length:128
        }))
    },
    onLeave(ret){
        console.log('24A59A4 ret', ret)
        console.log(hexdump(this.a6))
        console.log(hexdump(this.a7))
    }
})

// try {
//     var instruction = Instruction.parse(base.add(0x00bd90dc));
//     console.log("Instruction at address: " + instruction.toString());
// } catch (e) {
//     console.log("Error parsing instruction: " + e.message);
// }


// // hook sub
var sub_024A641C = base.add(0x024A641C);
Interceptor.attach(sub_024A641C, {
    onEnter(args) {
        this.a1 = args[0]
        this.a2 = args[1];
        this.a6 = args[6];
        this.a7 = args[7];
        this.a8 = args[8]
        // console.log('sub_024A641C', args[0], args[1], args[2], args[5])
        console.log(`sub_024A641C len: ${args[2]} a2`,hexdump(args[1]))
        console.log(`sub_024A641C len: ${args[4]} secData`,hexdump(args[3]))
        console.log('a6', args[5])
    },
    onLeave(retval) {
        console.log('sub_024A641Cretval', retval)
        console.log('key\n', hexdump(this.a6))
        console.log('body\n', hexdump(this.a7.readPointer()))
        console.log('postKey\n', hexdump(this.a8.readPointer(), {length: 33})) // key
        console.log(this.a6, this.a6.readPointer())
    }
})
// //


var sub_1024A6C6C = base.add(0x024A6C6C)
Interceptor.attach(sub_1024A6C6C, {
    onEnter(args) {
        console.log('sub_1024A6C6C')
        console.log('sub_1024A6C6C a1', hexdump(args[0]))
        console.log('sub_1024A6C6C a2', hexdump(args[1]))
        this.a1 = args[1];
        console.log('sub_1024A6C6C a3', args[2])
        console.log('sub_1024A6C6C a4', hexdump(args[3]))
        console.log('sub_1024A6C6C a4', hexdump(args[3].readPointer()))
        this.a3 = args[3];
        console.log('sub_1024A6C6C a5', args[4])
        this.a4 = args[4];

    },
    onLeave(ret) {
        console.log('sub_1024A6C6C.leave', ret)
        // console.log('a4\n', hexdump(this.a4.readPointer()))
    }
})
// //
var sub_1024A4E68 = base.add(0x024A4E68);
Interceptor.attach(sub_1024A4E68, {
    onEnter(args) {
        this.a1 = args[0]
        console.log('1024A4E68.in a1', hexdump(this.a1))
        console.log('1024A4E68.in a2', hexdump(args[1]))
        console.log('1024A4E68.in a3', args[2])


    },
    onLeave(ret) {
        console.log('1024A4E68.leave a1', hexdump(ret))
        console.log('1024A4E68.leave a1', hexdump(this.a1))
    }
})

var sub_024A4F60 = base.add(0x024A4F60);

Interceptor.attach(sub_024A4F60, {
    onEnter(args) {
        this.a1 = args[0]
        console.log('sub_024A4F60.in a2', hexdump(this.a1))
        console.log('sub_024A4F60.in a2', hexdump(args[1]))
        console.log('sub_024A4F60.in a3', args[2])


    },
    onLeave(ret) {
        console.log('sub_024A4F60.leave ret', ret)
        console.log('sub_024A4F60.leave a1', hexdump(this.a1.readPointer()))
    }
})
// -----BEGIN PUBLIC KEY-----
//     MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEzQPKEotPVsfHrVqlUJItgDGc/8r+
// T5e1xEnnlDjrOdJB9uHPN8tI8WZhRbt4EBaDPB3Zi5iZeQhpXIejp0gXUQ==
// -----END PUBLIC KEY-----
// let sub_24A50F4 = base.add(0x24A50F4);
// Interceptor.attach(sub_24A50F4, {
//     onEnter(args) {
//         this.a1 = args[0]
//         console.log('sub_24A50F4.in a0', hexdump(this.a1.readPointer()))
//         console.log('sub_24A50F4.in a1', args[1])
//
//
//
//
//     },
//     onLeave(ret) {
//         // console.log('sub_024A4F60.leave a1', hexdump(ret))
//         console.log('sub_24A50F4.leave a1', hexdump(this.a1.readPointer()))
//     }
// })


let sub_24A504C = base.add(0x24A504C);
Interceptor.attach(sub_24A504C, {
    onEnter(args){
        this.a1 = args[0]
        this.a3 = args[2]
        console.log('sub_24A504C.in a1', hexdump(this.a1))
        console.log('sub_24A504C.in a2', hexdump(args[1]))
        console.log('sub_24A504C.in a3', hexdump(args[2]))
    },
    onLeave(ret){
        console.log('sub_24A504C.leave a0', ret)
        console.log('sub_24A504C.leave a1', hexdump(this.a1))
        console.log('sub_24A504C.leave a3', hexdump(this.a3))
    }
})

let sub_24A5878 = base.add(0x24A5878);
Interceptor.attach(sub_24A5878, {
    onEnter(args){
        this.a1 = args[0]
        console.log('sub_24A5878.in a1', hexdump(this.a1))
        console.log('sub_24A5878.in a2', hexdump(args[1]))
        console.log('sub_24A5878.in a3', args[2])
        // console.log(Thread.backtrace(this.context, Backtracer.ACCURATE)
        // .map(DebugSymbol.fromAddress).join('\n') + '\n')
    },
    onLeave(ret){
        console.log('sub_24A5878.leave ret', ret)
        console.log('sub_24A5878.leave a0', hexdump(this.a1))
    }
})

let sub_24A6774 = base.add(0x24A6774);
Interceptor.attach(sub_24A6774, {
    onEnter(args){
        this.a1 = args[0]
        this.a2 = args[1]
        this.a3 = args[2]
        this.a4 = args[3]
        this.a5 = args[4]
        console.log('sub_24A6774.in a1', hexdump(this.a1))
        console.log('sub_24A6774.in a2', hexdump(this.a2))
        console.log('sub_24A6774.in a3', this.a3)
        console.log('sub_24A6774.in a4', hexdump(args[3]))
        console.log('sub_24A6774.in a5', hexdump(args[4]))
        console.log('sub_24A6774.in a6', args[5])
        // console.log(Thread.backtrace(this.context, Backtracer.ACCURATE)
        // .map(DebugSymbol.fromAddress).join('\n') + '\n')
    },
    onLeave(ret){
        console.log('sub_24A6774.leave ret', ret)
        console.log('sub_24A6774.leave a4', hexdump(this.a4))
        console.log('sub_24A6774.leave a5', hexdump(this.a5))
    }
})


let sub_E9B670 = base.add(0xE9B670);
Interceptor.attach(sub_E9B670, {
    onEnter(args){
        this.a1 = args[0]
        this.a2 = args[1]
        this.a3 = args[2]
        console.log('sub_E9B670.in a1', hexdump(this.a1))
        console.log('sub_E9B670.in a2', this.a2)
        console.log('sub_E9B670.in a3', args[2])
        // console.log(Thread.backtrace(this.context, Backtracer.ACCURATE)
        // .map(DebugSymbol.fromAddress).join('\n') + '\n')
    },
    onLeave(ret){
        console.log('sub_E9B670.leave ret', hexdump(ret.readPointer()))
        // console.log(Thread.backtrace(this.context, Backtracer.ACCURATE)
        // .map(DebugSymbol.fromAddress).join('\n') + '\n')
        // console.log('sub_E9B670.leave a4', hexdump(this.a4))
        // console.log('sub_E9B670.leave a5', hexdump(this.a5))
    }
})

// let sub_EC6940 = base.add(0xEC6940);
// Interceptor.attach(sub_EC6940, {
//     onEnter(args){
//         this.a1 = args[0]
//         this.a2 = args[1]
//         this.a3 = args[2]
//         this.a4 = args[3];
//         this.a5 = args[4];
//         this.a6 = args[5];

//         console.log('sub_EC6940.in a1', hexdump(this.a1.readPointer()))
//         console.log('sub_EC6940.in a2', hexdump(this.a2.readPointer()))
//         console.log('sub_EC6940.in a3', this.a3)
//         console.log('sub_EC6940.in a3',  hexdump(this.a3))
//         console.log('sub_EC6940.in ', this.a4, this.a5, this.a6);
        
//         // console.log(Thread.backtrace(this.context, Backtracer.ACCURATE)
//         // .map(DebugSymbol.fromAddress).join('\n') + '\n')
//     },
//     onLeave(ret){
//         console.log('sub_EC6940.leave ret', ret)
//         console.log('sub_EC6940.leave a4', this.a4)
//         console.log('sub_EC6940.leave a5', this.a5)
//     }
// })


var addr2 = 0x24A533C;
Interceptor.attach(base.add(addr2), {
    onEnter: function(args) {
        var tid = Process.getCurrentThreadId();
        this.tid = tid;
        Stalker.follow(tid, {
            events: {
                call: true, // CALL instructions: yes please
                ret: false, // RET instructions
                exec: false, // all instructions: not recommended as it's
                block: false, // block executed: coarse execution trace
                compile: false // block compiled: useful for coverage
            },
            transform: (iterator) => {
                let instruction = iterator.next();
                const startAddress = instruction.address;
                var isAppCode = startAddress.compare(base.add(addr2)) >= 0 && startAddress.compare(base.add(addr2).add(10000)) === -1;
                do {
                    if (isAppCode) {
                        // console.log(instruction.address.sub(base), instruction, '|', instruction.regsRead, '|', instruction.regsWritten);


                        // if (instruction.address.sub(base).equals(0x24a5574) ) {
                        //     iterator.putCallout((ctx, data) => {
                        //         console.log('x8', hexdump(ctx.x8));


                        //     });

                        // }


                        if (instruction.address.sub(base).equals(0x24a5368) ) {
                            // console.log('regsRead',instruction.x9, instruction.x11)
                            iterator.putCallout((ctx, data) => {
                                console.log('a1', hexdump(ctx.x0.readPointer()));
                                console.log('a2', hexdump(ctx.x1));
                                console.log('a3', hexdump(ctx.x2));

                                console.log('x8',ctx.sp);
                                console.log('x8',hexdump(ctx.sp));
                            });
                        }

                        if (instruction.address.sub(base).equals(0x24a536c) ) {
                            // console.log('regsRead',instruction.x9, instruction.x11)
                            iterator.putCallout((ctx, data) => {
                                console.log('0x24a536c a1', ctx.x0);

                            });
                        }


                        if (instruction.address.sub(base).equals(0x24a5370) ) {
                            // console.log('regsRead',instruction.x9, instruction.x11)
                            iterator.putCallout((ctx, data) => {
                                console.log(addr2,'q0', hexdump(ctx.q0));
                                console.log(addr2,'q1', hexdump(ctx.q1));
                                console.log(addr2,'s0', ctx.sp);

                            });
                        }
                        if (instruction.address.sub(base).equals(0x24a5374) ) {
                            // console.log('regsRead',instruction.x9, instruction.x11)
                            iterator.putCallout((ctx, data) => {
                                console.log(addr2,'q0', hexdump(ctx.q0));
                                console.log(addr2,'q1', hexdump(ctx.q1));

                            });
                        }
                        if (instruction.address.sub(base).equals(0x24a5378) ) {
                            // console.log('regsRead',instruction.x9, instruction.x11)
                            iterator.putCallout((ctx, data) => {
                                console.log(addr2,'q0', hexdump(ctx.q0));
                                console.log(addr2,'q1', hexdump(ctx.q1));

                            });
                        }

                        // if (instruction.address.sub(base).equals(0x24a50ac) ) {
                        //     // console.log('regsRead',instruction.x9, instruction.x11)
                        //     iterator.putCallout((ctx, data) => {
                        //             console.log("w8 = " + ctx.x8.sub(base))
                        //             console.log('w0 = ', hexdump(ctx.x0));
                        //             console.log('w1 = ', hexdump(ctx.x1));
                        //             console.log('w2 = ', hexdump(ctx.x2));

                        //     });
                        // }
                        // if (instruction!.mnemonic === "bl") {
                        //
                        // }
                    }
                    iterator.keep();
                } while ((instruction = iterator.next()) !== null);

            }
        })
    }, onLeave: function(retval) {
        Stalker.unfollow(this.tid);
        Stalker.garbageCollect();
        // console.log("retval:", new ObjC.Object(retval))
        // console.log(addr.toString(16), "= loginWithPhoneNbr onLeave =");
    }
});




var addr = 0x024A4E68;
Interceptor.attach(base.add(addr), {
    onEnter: function(args) {
        var tid = Process.getCurrentThreadId();
        this.tid = tid;
        Stalker.follow(tid, {
            events: {
                call: true, // CALL instructions: yes please
                ret: false, // RET instructions
                exec: false, // all instructions: not recommended as it's
                block: false, // block executed: coarse execution trace
                compile: false // block compiled: useful for coverage
            },
            transform: (iterator) => {
                let instruction = iterator.next();
                const startAddress = instruction.address;
                var isAppCode = startAddress.compare(base.add(addr)) >= 0 && startAddress.compare(base.add(addr).add(10000)) === -1;
                do {
                    if (isAppCode) {
                        console.log(instruction.address.sub(base), instruction, '|', instruction.regsRead, '|', instruction.regsWritten);


                        // if (instruction.address.sub(base).equals(0x24a4ee4) ) {
                        //     iterator.putCallout((ctx, data) => {
                        //         console.log('x8', hexdump(ctx.x8));


                        //     });

                        // }
                        if (instruction.address.sub(base).equals(0x24a4ee0) ) {
                            // console.log('regsRead',instruction.x9, instruction.x11)
                            iterator.putCallout((ctx, data) => {
                                console.log('q0', hexdump(ctx.q0));
                                console.log('q1', hexdump(ctx.q1));
                                console.log('s0', ctx.sp);

                            });
                        }
                        if (instruction.address.sub(base).equals(0x24a4ee4) ) {
                            // console.log('regsRead',instruction.x9, instruction.x11)
                            iterator.putCallout((ctx, data) => {
                                console.log('q0', hexdump(ctx.q0));
                                console.log('q1', hexdump(ctx.q1));

                            });
                        }
                        if (instruction.address.sub(base).equals(0x24a4ee8) ) {
                            // console.log('regsRead',instruction.x9, instruction.x11)
                            iterator.putCallout((ctx, data) => {
                                console.log('q0', hexdump(ctx.q0));
                                console.log('q1', hexdump(ctx.q1));

                            });
                        }

                        // if (instruction.address.sub(base).equals(0x24a50ac) ) {
                        //     // console.log('regsRead',instruction.x9, instruction.x11)
                        //     iterator.putCallout((ctx, data) => {
                        //             console.log("w8 = " + ctx.x8.sub(base))
                        //             console.log('w0 = ', hexdump(ctx.x0));
                        //             console.log('w1 = ', hexdump(ctx.x1));
                        //             console.log('w2 = ', hexdump(ctx.x2));

                        //     });
                        // }
                        // if (instruction!.mnemonic === "bl") {
                        //
                        // }
                    }
                    iterator.keep();
                } while ((instruction = iterator.next()) !== null);
            }
        })
    }, onLeave: function(retval) {
        Stalker.unfollow(this.tid);
        Stalker.garbageCollect();
        // console.log("retval:", new ObjC.Object(retval))
        // console.log(addr.toString(16), "= loginWithPhoneNbr onLeave =");
    }
});




var addr = 0x24A504C;
Interceptor.attach(base.add(0x24A504C), {
    onEnter: function(args) {
        var tid = Process.getCurrentThreadId();
        this.tid = tid;
        Stalker.follow(tid, {
            events: {
                call: true, // CALL instructions: yes please
                ret: false, // RET instructions
                exec: false, // all instructions: not recommended as it's
                block: false, // block executed: coarse execution trace
                compile: false // block compiled: useful for coverage
            },
            transform: (iterator) => {
                let instruction = iterator.next();
                const startAddress = instruction.address;
                var isAppCode = startAddress.compare(base.add(addr)) >= 0 && startAddress.compare(base.add(addr).add(10000)) === -1;
                do {
                    if (isAppCode) {
                        // console.log(instruction.address.sub(base), instruction, '|', instruction.regsRead, '|', instruction.regsWritten);

                        // if (instruction.address.sub(base).equals(0x24a507c) ) {
                        //     // console.log('regsRead',instruction.x9, instruction.x11)
                        //     iterator.putCallout((ctx, data) => {
                        //             console.log('a2', hexdump(ctx.x20));
                        //     });
                        // }
                        if (instruction.address.sub(base).equals(0x24a5084) ) {
                            // console.log('regsRead',instruction.x9, instruction.x11)
                            iterator.putCallout((ctx, data) => {
                                console.log("w9 = " + ctx.x9, '^', ctx.x11, '=',  ctx.x9.xor(ctx.x11))
                            });
                        }

                        if (instruction.address.sub(base).equals(0x24a50ac) ) {
                            // console.log('regsRead',instruction.x9, instruction.x11)
                            iterator.putCallout((ctx, data) => {
                                console.log("w8 = " + ctx.x8.sub(base))
                                console.log('w0 = ', hexdump(ctx.x0));
                                console.log('w1 = ', hexdump(ctx.x1));
                                console.log('w2 = ', hexdump(ctx.x2));

                            });
                        }
                        // if (instruction!.mnemonic === "bl") {
                        //
                        // }
                    }
                    iterator.keep();
                } while ((instruction = iterator.next()) !== null);
            }
        })
    }, onLeave: function(retval) {
        Stalker.unfollow(this.tid);
        Stalker.garbageCollect();
        // console.log("retval:", new ObjC.Object(retval))
        // console.log(addr.toString(16), "= loginWithPhoneNbr onLeave =");
    }
});


